package com.ww.httpclient;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.hc.core5.http.protocol.HttpContext;

import java.io.IOException;

/**
 * 会话保持
 */
public class HttpClientSessionDemo {

    public static void main(String[] args) throws IOException {
        /**
         * 在HttpClient中提供了HttpClientContext类，可以实现会话保持功能
         */
        HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute("name", "张三");
        HttpClientContext httpClientContext = HttpClientContext.adapt(httpContext);

        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 登录
        httpClient.execute(new HttpPost(""), httpClientContext);
        // 获取数据
        httpClient.execute(new HttpGet(""), httpClientContext);
    }
}
